import {fetch, post} from '../../../utils/axios'

const state = {
  name: '项目列表',
  editdialog: false,
  svgDialog: false,
  initDialog: false,
  infoDialog: false,
  value: 0,
  parent: {},
  project_nature: {},
  projectType: {},
  status: {},
  publish_type: {},
  build_param: {},
  pub_version: {},
  detection: {},
  tempHeader: [],
  env: {},
  owner: {},
  id: null,
  search: '',
  totalItems: 0,
  items: [],
  loading: true,
  pagination: {},
  headers: [],
  graph: { nodes: [], links: [] },
  svgTable: {},
  etext: 'No Data',
  rowsPerPageItems: [10, 50, 100, { text: 'All', value: 100000 }],
  searchLabel: 'name,name_en',
  openTree: true
}

const mutations = {
  handlerProject: (state) => {
    const { sortBy, descending, page, rowsPerPage } = state.pagination
    let ordering = descending ? '-' + sortBy : sortBy
    fetch('/project/', {
      offset: (page - 1) * rowsPerPage,
      limit: rowsPerPage,
      ordering,
      search: state.search,
      env: state.env.value
    }).then((response) => {
      state.items = response.results
      state.totalItems = response.count
      state.loading = false
    }).catch((error) => {
      state.etext = error.response
    })
  },
  openProjectEdit (state, payload) {
    state.id = payload.id
    state.editdialog = true
  },
  loadProjectFilters (state) {
    fetch('/project/project_filters/', {
    }).then((response) => {
      let res = JSON.parse(response)
      state.env.items = res.env
      state.parent.items = res.parent
      state.project_nature.items = res.project_nature
      state.projectType.items = res.projectType
      state.status.items = res.status
      state.publish_type.items = res.publish_type
      state.owner.items = res.owner
      state.build_param.items = res.build_param
      state.pub_version.items = res.pub_version
      state.detection.items = res.detection
    }).catch((error) => {
      state.etext = error.response
    })
  },
  loadProjectHeaders (state) {
    fetch('/getmodels/', {
      appname: 'project',
      modelname: 'Project'
    }).then((response) => {
      state.headers = [{text: '项目名-项目英文名-环境-负责人-状态-操作', value: 'name', align: 'center', sortable: false}]
      state.tempHeader = JSON.parse(response)
      // state.headers.splice(6, 0, {text: 'Actions', align: 'center', sortable: false})
      // state.headers.splice(0, 0, {text: '', align: 'center', sortable: false})
    }).catch((error) => {
      state.etext = error.response
    })
  },
  showSvg (state, payload) {
    let url = '/project/get_svg/'
    fetch(url, {
      opt: payload
    }).then((response) => {
      state.graph = JSON.parse(response)
      state.svgDialog = true
    }).catch((error) => {
      alert(error.response)
    })
  },
  showSvgTable (state, payload) {
    let url = '/project/get_svgTable/'
    fetch(url, {
      opt: payload
    }).then((response) => {
      state.svgTable = JSON.parse(response)
    }).catch((error) => {
      alert(error.response)
    })
  },
  initHost (state, payload) {
    let url = '/project/back_init_hosts/'
    post(url, {
      opt: payload
    }).then((response) => {
      alert(JSON.parse(response).data)
      this.commit('listproject/handlerProject')
    }).catch((error) => {
      alert(error.response.data.error)
    })
  },
  saltInfo (state, payload) {
    state.id = payload
    state.infoDialog = true
  }
}

const actions = {
  handlerProject: ({commit}) => {
    state.loading = true
    commit('handlerProject')
  },
  projectSearch ({commit, state}) {
    state.loading = true
    commit('handlerProject')
  },
  openProjectEdit ({commit}, payload) {
    commit('openProjectEdit', payload)
  },
  loadProjectFilters ({commit}) {
    commit('loadProjectFilters')
  },
  loadProjectHeaders ({commit}) {
    commit('loadProjectHeaders')
  },
  showSvg ({commit}, payload) {
    commit('showSvg', payload)
    commit('showSvgTable', payload)
  },
  showSvgTable ({commit}, payload) {
    commit('showSvgTable', payload)
  },
  initHost ({commit}, payload) {
    let r = confirm('确认要初始化?')
    if (r === true) {
      commit('initHost', payload)
    }
  },
  saltInfo ({commit}, payload) {
    commit('saltInfo', payload)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
